<?php
header('Content-Type: application/json');
	require_once("util.php");
	require_once("sql.php");
	require_once("document.php");
?>

<?php

if($_SERVER['REQUEST_METHOD'] == "GET"){

	$statutories=array();
	$responseJson="";
	
	//Get the fulldetils of company(for display in table)
	if (array_key_exists("fulldetails",$_REQUEST)){
		$responseJson .="[";
		
			$util=new DBUtil();
			$conn=$util->getConnection();
			$sql = SQLQuery::DOCUMENT_ALL_SQL;
			$stmt = $conn->prepare($sql);
			$stmt->execute();
			
			$result = $stmt->get_result();
						
			if ($result->num_rows > 0) {
							while($row = $result->fetch_array()) {
												
							$responseJson .="{";
							$responseJson .= '"id":"' .$row["ID"] .'",';
							$responseJson .= '"name":"' .$row["NAME"] .'",';
							$responseJson .= '"description":"' .$row["DESCRIPTION"] .'",';
							$responseJson .= '"period":"' .$row["PERIOD"] .'"';
							$responseJson .="},";
							}
					$responseJson=substr($responseJson,0,strlen($responseJson)-1);
			}
			$conn->close();
		
			$responseJson .="]";
	
	}
	else {
		//for update & delete operation, get the information about the selected company
		if (array_key_exists("key",$_REQUEST)){
			$documentPK= $_REQUEST["key"];
			
			$util=new DBUtil();
			$conn=$util->getConnection();
			$sql = SQLQuery::DOCUMENT_FIND_BYID;
			$stmt = $conn->prepare( $sql);
			$stmt->bind_param('d',$documentPK);
			$stmt->execute();
			
			$result = $stmt->get_result();
			
			if ($result->num_rows > 0) {
				while($row = $result->fetch_array()) {
					$document_name =$row["NAME"];
				$document_description=$row["DESCRIPTION"];
				$document_period=$row["PERIOD"];
				
				$periods = array();
				array_push($periods,'When Required');
				array_push($periods,'Monthly');
				array_push($periods,'Quaterly');
				array_push($periods,'Half Yearly');
				array_push($periods,'Yearly');
				
				$periodsJsonList=array();
				foreach($periods as $period)
				{
					$periodsJsonString ='{';
					$periodsJsonString .= '"op_value":"' .$period .'",';
					$periodsJsonString .= '"op_label":"' .$period .'",';
					
					if($period == $document_period)
					{
						$periodsJsonString .= '"op_selected":"selected"';					
					}else
					{
						$periodsJsonString .= '"op_selected":""';
					}
					$periodsJsonString .='}';
					array_push($periodsJsonList,$periodsJsonString);
				}
				
				$periodjsonStr=implode(",",$periodsJsonList);
				
				$responseJson='[{"id":"document_name","label":"Document Name","display":"true","value":"' .$document_name .'","type":"input_text"} ,{"id":"document_desc","label":"Document Description","display":"true","value":"' .$document_description .'","type":"input_text"},{"id":"document_period","label":"Document Period","display":"true","type":"input_select","value":['.$periodjsonStr .']}]';
				
				
				}
			}
			$conn->close();	
		}
		else {//For create operation
			$responseJson='[{"id":"document_name","label":"Document Name","display":"true","type":"input_text"}, {"id":"document_desc","label":"Document Description","display":"true","type":"input_text"},{"id":"document_period","label":"Document Period","display":"true","type":"input_select","value":[{"op_value":"When Required","op_label":"When Required"},{"op_value":"Monthly","op_label":"Monthly"},{"op_value":"Quaterly","op_label":"Quaterly"},{"op_value":"Half Yearly","op_label":"Half Yearly"},{"op_value":"Yearly","op_label":"Yearly"}]}]';
		}
	}
	echo $responseJson;
}
elseif ($_SERVER['REQUEST_METHOD'] == "POST"){

	if (array_key_exists("action",$_REQUEST)){
		$action= $_REQUEST["action"];
		
		$success ='{"status" :"success"}';
		$failure ='{"status" :"faiure"}';
		
		if($action=="create"){
			$documentManager=new ManageDocument();
			$status=$documentManager->addDocument($_REQUEST["document_name"],$_REQUEST["document_desc"],$_REQUEST["document_period"]);
			if($status){echo $success;}
			else {echo $failure;}
		}elseif($action=="update"){
			$documentManager=new ManageDocument();
			$status=$documentManager->updateDocument($_REQUEST["key"],$_REQUEST["document_name"],$_REQUEST["document_desc"],$_REQUEST["document_period"]);
			if($status){echo $success;}
			else {echo $failure;}
		}else if($action=="delete"){
			$documentManager=new ManageDocument();
			$status=$documentManager->deleteDocument($_REQUEST["key"]);
			if($status){echo $success;}
			else {echo $failure;}
		}else{
			echo $failure;
		}
	}
	else{
		echo $failure;
	}

}

/*---------------------------------------*/

class ManageDocument {
	
	
	public function __construct(){
		}
	public  function addDocument($document_name,$document_desc,$document_period){
		$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::DOCUMENT_ADD;
		$stmt = $conn->prepare($sql);
		$stmt->bind_param('sss',$document_name,$document_desc,$document_period);
		$status=$stmt->execute();
		
		$conn->commit();
		$conn->close();
		return $status;
	}
	
	public  function updateDocument($id,$name,$description,$period){
		
		$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::DOCUMENT_UPDATE;
		$stmt = $conn->prepare( $sql);
		$stmt->bind_param('sssd',$name,$description,$period,$id);
		$status=$stmt->execute();
		
		$conn->commit();
		$conn->close();
		return $status;
	}
	public function deleteDocument($id){
		
		$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::DOCUMENT_DELETE;
		$stmt = $conn->prepare( $sql);
		$stmt->bind_param('d',$id);
		$status=$stmt->execute();
		
		$conn->commit();
		$conn->close();
		return $status;
	}
	
}

?>